package swipe;

import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;

public class SwipeTouchView extends View {

    private final SwipeGesture swipeGesture;

    public SwipeTouchView(Context context, boolean validator, final SwipeIndicator swipeIndicator, SwipeIndicator.OnSwipe onSwipe, OnReachBack onReachBack) {
        this(context, validator, swipeIndicator, onSwipe, false, onReachBack);
    }

    public SwipeTouchView(Context context, final boolean validator, final SwipeIndicator swipeIndicator, final SwipeIndicator.OnSwipe onSwipe, final boolean isRight, final OnReachBack onReachBack) {
        super(context);
        this.swipeGesture = new SwipeGesture(this, new SwipeGesture.OnGesture() {
            @Override
            public void onGesture(float f1, float f2) {
                if (!isRight) {
                    swipeIndicator.maskPainterL.reset();
                    swipeIndicator.maskPainterL.b(f1);
                    swipeIndicator.maskPainterL.a(f2);
                    swipeIndicator.arrowPainterL.reset();
                    swipeIndicator.arrowPainterL.a(f2);
                    swipeIndicator.arrowPainterL.b().set(swipeIndicator.maskPainterL.b());
                } else {
                    swipeIndicator.maskPainterR.reset();
                    swipeIndicator.maskPainterR.b(f1);
                    swipeIndicator.maskPainterR.a(f2);
                    swipeIndicator.arrowPainterR.reset();
                    swipeIndicator.arrowPainterR.a(f2);
                    swipeIndicator.arrowPainterR.b().set(swipeIndicator.maskPainterR.b());
                }
                swipeIndicator.invalidate();
            }

            @Override
            public void onReachBack() {
                if (onReachBack != null) {
                    onReachBack.onReachBack();
                }
            }
        }, new SwipeIndicator.OnSwipe() {
            @Override
            public void onSwipe() {
                onSwipe.onSwipe();
            }
        }, validator);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return swipeGesture.a(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
    }

    public interface OnReachBack {
        void onReachBack();
    }

}
